#encoding:utf8

from django.db import models
from collections import OrderedDict

# Create your models here.

from config import mysql_info
from utils.tools import dt_format


class Tasker(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(null=False, blank=False, max_length=255, default="", help_text="任务标志")
    func = models.CharField(null=False, blank=False, max_length=255, help_text="函数名")
    args = models.CharField(null=False, blank=False, max_length=255, default="", help_text="位置或者字典参数")
    priority = models.IntegerField(null=False, blank=True, default=5, help_text="优先级; 1-10, default: 5")

    cron_type = models.CharField(null=False, blank=False, max_length=50, default="cron", help_text="定时任务类型")
    cron_params = models.CharField(null=False, blank=False, max_length=255, default='', help_text="定时任务参数")

    pid = models.CharField(null=False, blank=False, max_length=50, default="", help_text="运行时pid")
    start_time = models.DateTimeField(null=False, default="1970-01-01", help_text="最后一次运行的开始时间")
    end_time = models.DateTimeField(null=True, help_text="最后一次运行的结束时间")
    info = models.TextField(help_text="返回的结果, 或者部分结果")

    status = models.IntegerField(null=False, blank=True, default=0, help_text="状态")
    last_update_time = models.DateTimeField(auto_now=True)
    created_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = mysql_info['table_name']

    def get_info(self):
        return dict(id=self.id, title=self.title, func=self.func, args=self.args,
                    cron_type=self.cron_type, cron_params=self.cron_params,
                    pid=self.pid, priority=self.priority,
                    start_time=dt_format(self.start_time),
                    end_time=dt_format(self.end_time), info=self.info,
                    last_update_time=dt_format(self.last_update_time),
                    created_time=dt_format(self.created_time),
                    status=self.status
                    )






